home *** CD-ROM | disk | FTP | other *** search
- Public Class DynControlForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents lblStatus As System.Windows.Forms.Label
- Friend WithEvents chkUpperCase As System.Windows.Forms.CheckBox
- Friend WithEvents btnCreateControls As System.Windows.Forms.Button
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.btnCreateControls = New System.Windows.Forms.Button()
- Me.chkUpperCase = New System.Windows.Forms.CheckBox()
- Me.lblStatus = New System.Windows.Forms.Label()
- Me.SuspendLayout()
- '
- 'btnCreateControls
- '
- Me.btnCreateControls.Location = New System.Drawing.Point(16, 24)
- Me.btnCreateControls.Name = "btnCreateControls"
- Me.btnCreateControls.Size = New System.Drawing.Size(136, 32)
- Me.btnCreateControls.TabIndex = 0
- Me.btnCreateControls.Text = "Create Controls"
- '
- 'chkUpperCase
- '
- Me.chkUpperCase.Location = New System.Drawing.Point(168, 64)
- Me.chkUpperCase.Name = "chkUpperCase"
- Me.chkUpperCase.Size = New System.Drawing.Size(240, 16)
- Me.chkUpperCase.TabIndex = 2
- Me.chkUpperCase.Text = "Convert keys to upper case"
- '
- 'lblStatus
- '
- Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
- Me.lblStatus.Location = New System.Drawing.Point(168, 24)
- Me.lblStatus.Name = "lblStatus"
- Me.lblStatus.Size = New System.Drawing.Size(336, 24)
- Me.lblStatus.TabIndex = 1
- '
- 'DynControlForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
- Me.ClientSize = New System.Drawing.Size(568, 269)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkUpperCase, Me.lblStatus, Me.btnCreateControls})
- Me.Name = "DynControlForm"
- Me.Text = "DynControlForm"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' create any number of controls dynamically
-
- Private Sub btnCreateControls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateControls.Click
- Dim answer As String
- Dim index As Integer
-
- answer = InputBox("How many controls?", "Dynamic Control Creation", "5")
- If answer = "" Then Exit Sub
-
- For index = 1 To CInt(answer)
- ' Create the textbox
- Dim tb As New TextBox()
- tb.Size = New System.Drawing.Size(400, 30)
- tb.Location = New System.Drawing.Point(50, 40 + index * 40)
- 'tb.Visible = True
- ' we need this to identify the control
- tb.Name = "TextBox #" & CStr(index)
- ' add to the controls collection
- Me.Controls.Add(tb)
-
- ' prepare event handlers
- AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
- AddHandler tb.TextChanged, AddressOf TextBox_TextChanged
- AddHandler tb.MouseEnter, AddressOf TextBox_MouseEnter
- AddHandler tb.MouseLeave, AddressOf TextBox_MouseLeave
- Next
- End Sub
-
- ' a generic control handler that caps the pressed key
-
- Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
- ' exit if no conversion to upper case is required
- If Not chkUpperCase.Checked Then Exit Sub
- ' exit if char doesn't have to be converted
- If Not Char.IsLower(e.KeyChar) Then Exit Sub
-
- ' insert the key there
- DirectCast(sender, TextBox).SelectedText = Char.ToUpper(e.KeyChar)
- ' signal that this key has been dealt with
- e.Handled = True
- End Sub
-
- ' generic MouseEnter handler that changes the background color
-
- Private Sub TextBox_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
- lblStatus.Text = "MouseEnter in " & CStr(CType(sender, TextBox).Name)
- DirectCast(sender, TextBox).BackColor = Color.Yellow
- End Sub
-
- ' generic MouseLeave handler that restores the background color
-
- Private Sub TextBox_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs)
- lblStatus.Text = "MouseLeave in " & CStr(CType(sender, TextBox).Name)
- DirectCast(sender, TextBox).BackColor = SystemColors.Window
- End Sub
-
- ' generic TextChanged event handler
-
- Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
- lblStatus.Text = "TextChanged in " & CStr(CType(sender, TextBox).Name)
- End Sub
- End Class
-